package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Owner;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.domain.Vo;
import cn.wolfcode.wolf2w.query.OwnerQuery;
import cn.wolfcode.wolf2w.repository.OwnerRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IOwnerService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class OwnerServiceImpl implements IOwnerService {
    @Autowired
    private OwnerRepository ownerRepository;

    @Autowired
    private MongoTemplate mongoTemplate;//可以操作数据库

    @Autowired
    private IDestinationService destinationService;//目的地服务类

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public Owner get(String id) {
        return null;//如果是空是就返回空值,否则返回对象
    }

    @Override
    public List<Owner> listAll() {
        return null;
    }

    @Override
    public void save(Owner owner ) {
        owner.setId(null);

    }
    @Override
    public void updateOwner(Owner owner) {
//        BeanUtils.copyProperties(owner);
        owner.setCountNum(owner.getCountNum());
        ownerRepository.save(owner);

    }

    @Override
    public void commentAdd(Owner comment) {

    }

    @Override
    public void addComment(Owner comment, UserInfo userInfo) {

    }

    @Override
    public int countNum(String id, String userId) {
        String key=null;
        if (!id.equals(userId)) {
             key= id + "countNum";
            int countNum = 0;
            countNum = countNum + 1;
            String strCountNum = countNum + "";
//            Vo vo = new Vo();
//            vo.setCountNum(countNum);
            stringRedisTemplate.opsForValue().set(key, strCountNum);
            Optional<Owner> byId = ownerRepository.findById(userId);
            Owner owner = byId.orElse(null);
            owner.setCountNum(countNum);
            this.updateOwner(owner);

        }
        String countNum = stringRedisTemplate.opsForValue().get(key);
        Integer intCountNum = Integer.valueOf(countNum);
        return  intCountNum;
    }
    // 添加游记评论操作







    @Override
    public Page<Owner> query(OwnerQuery qo) {
        //查询对象
        Query query = new Query();
        //根据id过滤出这个攻略下的所有的评论
        if(StringUtils.hasLength(qo.getUserId())) {
            Criteria criteria = Criteria.where("travelId").is(qo.getUserId());
            query.addCriteria(criteria);
        }
        //查询出总数条数
        long rows = mongoTemplate.count(query, Owner.class);
        if (rows == 0) {
            return  Page.empty();//如果总数条数是0,就返回空集合
        }
        //分页对象(设置当前页从0开始算,还有页面显示条数,按sequence这个列正序排列 )  
        //  Sort.Direction.ASC,表示按正序排列表格
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1,
                8, Sort.Direction.DESC, "time");
        //给查询对象
        query.with(pageable);


        List<Owner> list = mongoTemplate.find(query, Owner.class);
        PageImpl<Owner> page = new PageImpl<>(list, pageable, rows);


        return page ;//相当于pageResult对象

    }



}
